home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 14.5 KB | 569 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: PRPictur.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef SLPALETE_H
- #include "SLPalete.h"
- #endif
-
- #ifndef FWODEXCE_H
- #include "FWODExce.h"
- #endif
-
- #ifndef FWBITMAP_H
- #include "FWBitmap.h"
- #endif
-
- #ifndef FWWINRES_H
- #include "FWWinRes.h"
- #endif
-
- #ifndef FWSTRMRW_H
- #include "FWStrmRW.h"
- #endif
-
- #ifndef FWRESOUR_H
- #include "FWResour.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef PRGDEV_H
- #include "PRGDev.h"
- #endif
-
- #ifndef FWSOMENV_H
- #include "FWSOMEnv.h"
- #endif
-
- #ifndef PRPICTUR_H
- #include "PRPictur.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWEXCEPT_H
- #include "FWExcept.h"
- #endif
-
- #ifndef SOM_FW_OResourceFile_xh
- #include "SLResFil.xh"
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
- #include <PictUtils.h>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_Picture
- #endif
-
- FW_DEFINE_AUTO(FW_CPrivPictureRep)
-
- //----------------------------------------------------------------------------------------
- // Placeable (Aldus) metafile definitions for Windows
- //----------------------------------------------------------------------------------------
-
- #ifdef FW_BUILD_WIN
-
- #pragma pack(push,1)
-
- struct RECT16
- {
- short left;
- short top;
- short right;
- short bottom;
- };
-
- struct SAldusHeader
- {
- DWORD key;
- WORD hmf;
- RECT16 bbox;
- WORD inch;
- DWORD reserved;
- WORD checksum;
- } ;
-
- #pragma pack(pop)
-
- const DWORD kAldusKey = 0x9AC6CDD7l;
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::FW_CPrivPictureRep
- //----------------------------------------------------------------------------------------
- // From Stream: we own the picture
-
- FW_CPrivPictureRep::FW_CPrivPictureRep(FW_CReadableStream& stream) :
- fOwnPicture(TRUE),
- fPlatformPict(NULL)
- {
- #ifdef FW_BUILD_MAC
- fMacLockCount = 0;
- #endif
-
- PrivReadFrom(stream);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::FW_CPrivPictureRep
- //----------------------------------------------------------------------------------------
- // From File: we own the picture
-
- FW_CPrivPictureRep::FW_CPrivPictureRep(FW_OResourceFile* resourceFile, FW_ResourceID resID) :
- fOwnPicture(TRUE),
- fPlatformPict(NULL)
- {
- FW_SOMEnvironment ev;
-
- #ifdef FW_BUILD_MAC
- FW_UNUSED(resourceFile);
-
- fMacLockCount = 0;
- FW_ASSERT(::CurResFile() == resourceFile->PrivGetResourceFileID(ev));
-
- FW_PlatformPict newPict = ::GetPicture(resID);
- if (newPict == NULL)
- {
- FW_FailOnError(::ResError());
- FW_Failure(FW_xMemoryExhausted);
- }
-
- // Because of CFM we have to detach the resource
- ::DetachResource((Handle)newPict);
- AdoptPlatformPict(newPict);
- #endif
-
- #ifdef FW_BUILD_WIN
- FW_PResource resource(ev, resourceFile, resID, FW_kPicture);
- FW_PResourceSink sink(ev, resource);
- FW_CReadableStream stream(sink);
- PrivReadFrom(stream);
- #endif
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::FW_CPrivPictureRep
- //----------------------------------------------------------------------------------------
- // From platform pict: we own the picture (no copy is made)
-
- FW_CPrivPictureRep::FW_CPrivPictureRep(FW_PlatformPict platformPict) :
- fOwnPicture(false),
- fPlatformPict(platformPict)
- {
- FW_ASSERT(platformPict != NULL);
-
- #ifdef FW_BUILD_MAC
- fMacLockCount = 0;
- #endif
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::FW_CPrivPictureRep
- //----------------------------------------------------------------------------------------
- // From another Pict: we own the picture (a copy is made)
-
- FW_CPrivPictureRep::FW_CPrivPictureRep(const FW_CPrivPictureRep& other) :
- fOwnPicture(TRUE),
- fPlatformPict(NULL)
- {
- FW_PlatformPict otherPict = other.GetPlatformPict();
-
- #ifdef FW_BUILD_MAC
- fPlatformPict = (FW_PlatformPict) FW_CMemoryManager::CopySystemHandle((FW_PlatformHandle)otherPict);
- fMacLockCount = 0;
- #endif
- #ifdef FW_BUILD_WIN
- fPlatformPict = ::CopyEnhMetaFile(otherPict, NULL);
- #endif
-
- if (fPlatformPict == 0)
- FW_Failure(FW_xMemoryExhausted);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::~FW_CPrivPictureRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivPictureRep::~FW_CPrivPictureRep()
- {
- FW_START_DESTRUCTOR
- PrivDisposePict();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::Write
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::Write(FW_CWritableStream& stream)
- {
- #ifdef FW_BUILD_MAC
- unsigned long picSize = FW_CMemoryManager::GetSystemHandleSize((FW_PlatformHandle)fPlatformPict);
- stream << picSize;
-
- MacLock();
-
- FW_TRY
- {
- stream.Write(*fPlatformPict, picSize);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- MacUnlock();
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- MacUnlock();
- #endif
- #ifdef FW_BUILD_WIN
- UINT bitsSize = ::GetEnhMetaFileBits(fPlatformPict, 0, NULL);
-
- BYTE* bitsBuf = new BYTE[bitsSize];
- ::GetEnhMetaFileBits(fPlatformPict, bitsSize, bitsBuf);
-
- FW_TRY
- {
- stream.Write(bitsBuf, bitsSize);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete[] bitsBuf;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- delete[] bitsBuf;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivPictureRep::IsEqual(const FW_CPrivPictureRep& other) const
- {
- return fPlatformPict == other.fPlatformPict;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::GetPictBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::GetPictBounds(FW_SRect& bounds) const
- {
- #ifdef FW_BUILD_MAC
-
- PictInfo pictInfo;
-
- if(::GetPictInfo(fPlatformPict, &pictInfo, 0, 0, 0, 0) != noErr)
- {
- pictInfo.sourceRect = (*fPlatformPict)->picFrame;
- }
-
- bounds.left = FW_kFixed0;
- bounds.top = FW_kFixed0;
- bounds.right = FW_IntToFixed(pictInfo.sourceRect.right - pictInfo.sourceRect.left);
- bounds.bottom = FW_IntToFixed(pictInfo.sourceRect.bottom - pictInfo.sourceRect.top);
-
- #endif
-
- #ifdef FW_BUILD_WIN
- ENHMETAHEADER emh;
- ::GetEnhMetaFileHeader(fPlatformPict, sizeof(emh), &emh);
-
- // emh.rclBounds is the bounding rectangle in pixels, computed by GDI
-
- bounds.left = FW_IntToFixed((int) emh.rclBounds.left);
- bounds.top = FW_IntToFixed((int) emh.rclBounds.top);
- bounds.right = FW_IntToFixed((int) emh.rclBounds.right);
- bounds.bottom = FW_IntToFixed((int) emh.rclBounds.bottom);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::GetPictBoundsGC
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::GetPictBoundsGC(Environment* ev, FW_SGraphicContext& gc, FW_SRect& bounds) const
- {
- #ifdef FW_BUILD_MAC
- FW_SPoint size;
-
- FW_SRect pictureBounds;
- GetPictBounds(pictureBounds);
- const long width = FW_FixedToInt(pictureBounds.right - pictureBounds.left);
- const long height = FW_FixedToInt(pictureBounds.bottom - pictureBounds.top);
-
- FW_PrivGC_DeviceToLogicalSize(ev, gc, width, height, size);
-
- // Let go if error
- bounds.left = FW_kFixed0;
- bounds.top = FW_kFixed0;
- bounds.right = size.x;
- bounds.bottom = size.y;
- #endif
-
- #ifdef FW_BUILD_WIN
- ENHMETAHEADER emh;
- ::GetEnhMetaFileHeader(fPlatformPict, sizeof(emh), &emh);
-
- // emh.rclFrame is the bounding rectangle as specified by the creator, in 0.01 mm units
-
- HDC hDC = gc.fGraphicDevice->GetPlatformCanvas();
-
- int xRes = ::GetDeviceCaps(hDC, LOGPIXELSX);
- int yRes = ::GetDeviceCaps(hDC, LOGPIXELSY);
-
- FW_CPlatformRect plfmBounds;
-
- plfmBounds.left = emh.rclFrame.left * xRes / 2540;
- plfmBounds.top = emh.rclFrame.top * yRes / 2540;
- plfmBounds.right = emh.rclFrame.right * xRes / 2540;
- plfmBounds.bottom = emh.rclFrame.bottom * yRes / 2540;
-
- FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmBounds, bounds);
- // Let go if error
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::GetPlatformPict
- //----------------------------------------------------------------------------------------
-
- FW_PlatformPict FW_CPrivPictureRep::GetPlatformPict() const
- {
- return fPlatformPict;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::IsPlatformPictOrphan
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivPictureRep::IsPlatformPictOrphan() const
- {
- return !fOwnPicture;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::OrphanPlatformPict
- //----------------------------------------------------------------------------------------
-
- FW_PlatformPict FW_CPrivPictureRep::OrphanPlatformPict()
- {
- FW_ASSERT(fOwnPicture); // Cannot orphan twice
- fOwnPicture = FALSE;
- return fPlatformPict;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::SetPlatformPict
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::SetPlatformPict(FW_PlatformPict newPict)
- {
- if (fPlatformPict != newPict)
- {
- PrivDisposePict();
- fPlatformPict = newPict;
- }
-
- fOwnPicture = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::AdoptPlatformPict
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::AdoptPlatformPict(FW_PlatformPict newPict)
- {
- SetPlatformPict(newPict);
- fOwnPicture = TRUE;
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::MacLock
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::MacLock()
- {
- FW_ASSERT(fPlatformPict != NULL);
-
- if (++ fMacLockCount == 1)
- FW_CMemoryManager::LockSystemHandle((FW_PlatformHandle)fPlatformPict);
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::MacUnlock
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::MacUnlock()
- {
- if (-- fMacLockCount == 0)
- FW_CMemoryManager::UnlockSystemHandle((FW_PlatformHandle)fPlatformPict);
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::PrivReadFrom
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::PrivReadFrom(FW_CReadableStream& stream)
- {
- #ifdef FW_BUILD_MAC
- unsigned long picSize;
- stream >> picSize;
-
- fPlatformPict = (FW_PlatformPict) FW_CMemoryManager::AllocateSystemHandle(picSize);
-
- MacLock();
-
- FW_TRY
- {
- stream.Read(*fPlatformPict, picSize);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- MacUnlock();
- ::DisposeHandle((Handle)fPlatformPict);
- fPlatformPict = NULL;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- MacUnlock();
- #endif
- #ifdef FW_BUILD_WIN
- HENHMETAFILE metafile = NULL;
-
- // Check if there is an Aldus metafile header
- SAldusHeader aldusHeader;
- stream.Read(&aldusHeader, sizeof(aldusHeader));
- if(aldusHeader.key == kAldusKey)
- {
- // ----- This is an Aldus format metafile
-
- // Read the header
- METAHEADER mfHeader;
- stream.Read(&mfHeader, sizeof(mfHeader));
-
- // Read the bits
- size_t bitsSize = mfHeader.mtSize * 2L;
- BYTE* bitsBuf = new BYTE[bitsSize];
-
- FW_TRY
- {
- FW_CMemoryManager::CopyMemory(&mfHeader, bitsBuf, sizeof(mfHeader));
- stream.Read(bitsBuf + sizeof(mfHeader), bitsSize - sizeof(mfHeader));
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete[] bitsBuf;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Convert an Aldus (Win16) metafile to a Win32 Enhanced Metafile
- METAFILEPICT mfPict;
- mfPict.mm = MM_ANISOTROPIC;
- mfPict.xExt = (aldusHeader.bbox.right - aldusHeader.bbox.left) * 2540 / aldusHeader.inch;
- mfPict.yExt = (aldusHeader.bbox.bottom - aldusHeader.bbox.top) * 2540 / aldusHeader.inch;
-
- metafile = ::SetWinMetaFileBits(bitsSize, bitsBuf, NULL, &mfPict);
- delete[] bitsBuf;
- }
- else
- {
- // ----- Not an Aldus metafile, try Win32 Enhanced metafile
-
- // Read the header
- ENHMETAHEADER enhHeader;
- FW_CMemoryManager::CopyMemory(&aldusHeader, &enhHeader, sizeof(aldusHeader));
- stream.Read((char*) &enhHeader + sizeof(aldusHeader), sizeof(enhHeader) - sizeof(aldusHeader));
-
- // Verify the header
- if(enhHeader.iType == EMR_HEADER || enhHeader.nSize == sizeof(enhHeader))
- {
- // Read the bits
- size_t bitsSize = enhHeader.nBytes;
- BYTE* bitsBuf = new BYTE[bitsSize];
- FW_TRY
- {
- FW_CMemoryManager::CopyMemory(&enhHeader, bitsBuf, sizeof(enhHeader));
- stream.Read(bitsBuf + sizeof(enhHeader), bitsSize - sizeof(enhHeader));
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete[] bitsBuf;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Create a metafile and set the bits
- metafile = ::SetEnhMetaFileBits(bitsSize, bitsBuf);
- delete[] bitsBuf;
- }
- }
-
- if(metafile == NULL)
- FW_Failure(FW_xInvalidPictureData);
-
- // Save metafile info
- AdoptPlatformPict(metafile);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPictureRep::PrivDisposePict
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPictureRep::PrivDisposePict()
- {
- if (fOwnPicture && fPlatformPict != NULL)
- {
- #ifdef FW_BUILD_MAC
- ::KillPicture(fPlatformPict);
- #endif
- #ifdef FW_BUILD_WIN
- ::DeleteEnhMetaFile(fPlatformPict);
- #endif
- }
-
- fPlatformPict = NULL;
- }
-